package cn.edu.stu.service;

import cn.edu.stu.bean.AjaxResult;;
import cn.edu.stu.bean.MicRecord;
import cn.edu.stu.bean.MicSong;
import cn.edu.stu.bean.MicUser;
import cn.edu.stu.mapper.MicArtistMapper;
import cn.edu.stu.mapper.MicRecordMapper;
import cn.edu.stu.mapper.MicSongMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author Karl
 * @Date 2022/12/16 - 20:42
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class SongService {
    @Autowired
    MicSongMapper songMapper;
    @Autowired
    MicArtistMapper artistMapper;

    @Autowired
    MicRecordMapper recordMapper;

    @Autowired
    ArtistService artistService;


    //有的话给我返回songID,不然为-1
    public int checkSongByMD5(String md5){
        Integer integer = songMapper.checkIdByMD5(md5);
//        TODO 假如已经上传过了，完成上传记录的插入

        return integer==null?-1:integer;
    }

    /**
     * 尝试使用MD5进行上传
     * @param md5 欲上传音频文件的MD5
     * @param record 记录对象
     * @param artistId 艺术家信息
     * @param artist 艺术家信息
     * @return
     */
    public boolean uploadSongWithMD5(String md5, MicRecord record, Integer artistId, String artist){
        int ret = checkSongByMD5(md5);
        if(ret==-1) return false;
        //已经有文件了，尝试插入record
        //查找艺术家的ID
        record.setSongId(ret);
        List<Integer> recordId = recordMapper.countWithSongIdAndUserId(ret, record.getUserId());
        if(recordId!=null&&recordId.size()!=0){
            throw new RuntimeException("你已经上传过该歌曲了");
        }
        if (!artistService.checkOrInsertArtistId(artistId,artist,record)) {
            throw new RuntimeException("缺少艺术家信息");
        }

        record.setUploadTime(System.currentTimeMillis());
        if (recordMapper.insert(record)!=1) {
            throw new RuntimeException("插入记录失败！");
        }

        return true;
    }


    /**
     * 往数据库插入数据行
     * @param song 歌曲文件，要得到他的ID
     * @param record 用户上传的记录为文件
     * @param artistId
     * @param artist
     * @return
     */
    public boolean uploadSong(MicSong song, MicRecord record, Integer artistId, String artist) {
        //获取艺术家的ID
        if (!artistService.checkOrInsertArtistId(artistId, artist,record)) {
            throw new RuntimeException("缺少艺术家信息");
        }
        int i = songMapper.insert(song);
        if(i==0) throw new RuntimeException("上传歌曲失败!");
        record.setSongId(song.getSongId());
        i = recordMapper.insert(record);
        if(i==0) throw new RuntimeException("上传歌曲失败!");

        return true;

    }

    public boolean deleteBySongId(Integer songId) {
        int result = songMapper.deleteByPrimaryKey(songId);
        if (result == 1) {
            recordMapper.deleteBySongId(songId);
        }
        return true;
    }

    public PageInfo<Map<String,Object>> getSongByPage(int pageNum, int pageSize, int pageToShow) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> users = recordMapper.selectAllRecord();
        System.out.println(users);
        return new PageInfo<>(users,pageToShow);
    }
}
